package com.scalar.chiptrack.operations.dataentry.lotscrap.actioncommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.dataentry.lotscrap.forms.ScrapMaterialForm;
import com.scalar.chiptrack.operations.dataentry.dao.ScrapMaterialDAO;
import com.scalar.chiptrack.operations.dataentry.dao.DataEntryDAO;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.commons.SessionKeys;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.util.LabelValueBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;



public class ScrapMaterialLoadLotDetailsCommand extends ActionCommand
{

    private static final String ACTION_NAME = "/scrapMaterial.do";

    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response ) throws Exception
    {
       ScrapMaterialForm scrapMaterialForm = ( ScrapMaterialForm ) form;
        String action                       = request.getParameter ( "action" );

        HashMap<String,String> errors                      = new HashMap<String,String>();
        HashMap info                        = new HashMap();

        request.setAttribute( INFO_MESSAGES, info );
        request.setAttribute( ERROR_MESSAGES, errors );
        String locationType = null;
        HttpSession mSession = request.getSession();
        User user            = ( User ) mSession.getAttribute ( SessionKeys.USER_KEY );

        if ( user == null )
        {
            mSession.setAttribute ( SessionKeys.SOURCE_PAGE_KEY,  ACTION_NAME );
            return ( mapping.findForward ( "login" ) );
        }
        else
        {
            if ( user.isActionAllowed ( Functionality.SCRAP_MATERIAL , User.USER_ACTION_MODIFY ) )
            {
                request.setAttribute ( "showMessage", "FALSE" );
            }
            else
            {
                request.setAttribute ( "showMessage", "TRUE" );
            }
        }
         ArrayList allRunningHoldLotsList;
         ArrayList vendorList = new ArrayList();
        try
        {
            locationType = NewProductDAO.getBaseProcessName( null, scrapMaterialForm.getProcessType() );

            scrapMaterialForm.setBaseprocessType( locationType );

            if ( locationType.equalsIgnoreCase( LOCATION_TYPE_FAB ) || locationType.equalsIgnoreCase( LOCATION_TYPE_WAFER_STORE )
                    || locationType.equalsIgnoreCase( LOCATION_TYPE_SORT ) || "Back Grind".equalsIgnoreCase(scrapMaterialForm.getProcessType() )
                    || locationType.equalsIgnoreCase( LOCATION_TYPE_DIE_BANK )  || locationType.equalsIgnoreCase(LOCATION_TYPE_BUMPING ) )
            {
                 request.setAttribute("showWaferIds", "TRUE");
            }
            else
            {
                request.setAttribute("showWaferIds", "FALSE");
            }

            if ( user.getVendorId() > 0 ) // for VENDOR INTERFACE
            {
                scrapMaterialForm.setLocationId(0);
                if (scrapMaterialForm.getLocationId() == 0 )
                {
                    vendorList =  VendorDAO.getInventoryLocationsByProcessByUserType( null, locationType , user.getVendorId() );
                    if ( vendorList != null && vendorList.size() > 0  )
                    {
                        scrapMaterialForm.setLocationId( Integer.parseInt ( ( (LabelValueBean)vendorList.get(0) ).getValue() ) );
                    }
                    else
                    {
                          scrapMaterialForm.setLocationId(-1);
                    }
                }
            }

            if (LOCATION_TYPE_DIE_BANK.equalsIgnoreCase(scrapMaterialForm.getProcessType()) )
            {
                if  (scrapMaterialForm.isNonValueInventory() )
                {
                      allRunningHoldLotsList = DataEntryDAO.getAllNonValueInventoryLots( scrapMaterialForm.getProcessType(), scrapMaterialForm.getProcessStepId() , scrapMaterialForm.getLocationId(), scrapMaterialForm.getLotId() ) ;
                }
                else
                {
                    allRunningHoldLotsList = DataEntryDAO.loadAllDieBankWipLots( scrapMaterialForm.getProcessStepId() , scrapMaterialForm.getLocationId(), scrapMaterialForm.getLotId(), false, false  );
                }
            }
            else
            {
                if  (scrapMaterialForm.isNonValueInventory() )
                {
                    allRunningHoldLotsList = DataEntryDAO.getAllNonValueInventoryLots( scrapMaterialForm.getProcessType(), scrapMaterialForm.getProcessStepId() , scrapMaterialForm.getLocationId(), scrapMaterialForm.getLotId() ) ;
                }
                else
                {
                    allRunningHoldLotsList = DataEntryDAO.getAllRunningHoldLots( scrapMaterialForm.getProcessType() , scrapMaterialForm.getProcessStepId() , scrapMaterialForm.getLocationId(), scrapMaterialForm.getLotId()  );
                }
            }

            if ( allRunningHoldLotsList == null || allRunningHoldLotsList.size() == 0 )
            {
                if ( scrapMaterialForm.isNonValueInventory() )
                {
                    info.put("No NonValueInvnetory lots available" , null );
                }
                else
                {
                    info.put("No WIP lots available" , null );
                }
            }

            scrapMaterialForm.setWipLotsList( allRunningHoldLotsList );
            request.setAttribute("lotsList", allRunningHoldLotsList);
            request.setAttribute("processTypes",    NewProductDAO.loadAllProcessesFromDB(null, false) );
            request.setAttribute("partNumbersList", ProductDao.loadAllPartsForProcessTypeByPsType( scrapMaterialForm.getProcessType() ) );
            request.setAttribute("locationList", VendorDAO.getInventoryLocationsByProcessByUserType( null, locationType, user.getVendorId() ) );

            request.setAttribute( "action", action );
        }
        catch( Exception e )
        {
            request.setAttribute("processTypes", NewProductDAO.loadAllProcessesFromDB(null, false) );
            errors.put( "Unable to get Inventory Lots For Scrap", null);
            e.printStackTrace();
        }
        return mapping.findForward( "scrapMaterial" );
     }
}
